package com.our.sys.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.set.service.HzService;
import com.our.sys.project1.dao.DMaterialsDao;
import com.our.sys.project1.dao.SMaterialsDao;
import com.our.sys.project1.entity.DMaterials;
import com.our.sys.project1.entity.SMaterials;

@Service
@Transactional
public class SMaterialsService {

    @Autowired
    private SMaterialsDao sMaterialsDao;
    @Autowired
    private DMaterialsDao dMaterialsDao;
    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(SMaterialsService.class);

    @Transactional(readOnly = true)
    public Page<SMaterials> queryS(Page<SMaterials> page, List<PropertyFilter> filters) {
        Page<SMaterials> curPage = sMaterialsDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public Page<DMaterials> queryD(Page<DMaterials> page, List<PropertyFilter> filters) {
        Page<DMaterials> curPage = dMaterialsDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public SMaterials querySById(Long id) {
        return sMaterialsDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    public DMaterials queryDById(Long id) {
        return dMaterialsDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByName(String name) {
        Long result = sMaterialsDao.findUnique("select count(a) from SMaterials a where a.name = ?", name);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    @Transactional(readOnly = true)
    private Long queryCountByNameAndSid(String name, Long sId) {
        Long result = dMaterialsDao.findUnique(
                "select count(b) from SMaterials a,DMaterials b where b.name = ? and a.id = ? and a.id = b.sId", name,
                sId);
        if (null == result) {
            return 0L;
        } else {
            return result;
        }
    }

    public void saveS(SMaterials sMaterials) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(sMaterials.getName());
            sMaterials.setPy(map.get("py"));
            sMaterials.setWb(map.get("wb"));
            if (null != sMaterials.getId()) {
                SMaterials getSMaterials = this.querySById(sMaterials.getId());

                if (queryCountByName(getSMaterials.getName()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个物资财务类别！");
                }

                getSMaterials.setName(sMaterials.getName());
                getSMaterials.setPy(sMaterials.getPy());
                getSMaterials.setWb(sMaterials.getWb());
                getSMaterials.setState(sMaterials.getState());
                sMaterialsDao.save(getSMaterials);
            } else {

                if (queryCountByName(sMaterials.getName()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个物资财务类别！");
                }
                sMaterialsDao.save(sMaterials);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void saveD(DMaterials dMaterials) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(dMaterials.getName());
            dMaterials.setPy(map.get("py"));
            dMaterials.setWb(map.get("wb"));
            if (null != dMaterials.getId()) {
                DMaterials getDMaterials = this.queryDById(dMaterials.getId());

                if (queryCountByNameAndSid(getDMaterials.getName(), dMaterials.getsId()).compareTo(1L) > 0) {
                    throw new Exception("已经存在这个子科目！");
                }

                getDMaterials.setName(dMaterials.getName());
                getDMaterials.setPy(dMaterials.getPy());
                getDMaterials.setWb(dMaterials.getWb());
                getDMaterials.setsId(dMaterials.getsId());
                getDMaterials.setState(dMaterials.getState());
                dMaterialsDao.save(getDMaterials);
            } else {

                if (queryCountByNameAndSid(dMaterials.getName(), dMaterials.getsId()).compareTo(0L) > 0) {
                    throw new Exception("已经存在这个子科目！");
                }
                dMaterialsDao.save(dMaterials);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteS(Long id) throws Exception {
        try {
            List<DMaterials> list = dMaterialsDao.find("from DMaterials where sId = ?", id);
            if (null != list && list.size() > 0) {
                throw new Exception("这个物资财务类别有对应子科目，不能删除！");
            } else {
                sMaterialsDao.delete(id);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void deleteD(Long id) throws Exception {
        try {
            dMaterialsDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
